package com.franklin.ideaplugin.api.beans.declaration.impl;

import com.franklin.ideaplugin.api.beans.declaration.IDeclaration;
import com.franklin.ideaplugin.api.beans.variable.IVariable;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

/**
 * 直接方法调用的声明
 *
 * @author Ye Junhui
 * @since 2024/3/8
 */
public class MethodCallExpressionDeclaration implements IDeclaration {

    private PsiMethodCallExpression psiMethodCallExpression;

    public MethodCallExpressionDeclaration(PsiMethodCallExpression psiMethodCallExpression) {
        this.psiMethodCallExpression = psiMethodCallExpression;
    }

    @Override
    public IVariable getVariable() {
        return null;
    }

    @Override
    public <T extends PsiExpression> T getCreateExpression(Class<T> expressionType) {
        if (!expressionType.equals(PsiMethodCallExpression.class)){
            return null;
        }
        return (T) this.psiMethodCallExpression;
    }

    @Override
    public Project getProject() {
        return psiMethodCallExpression.getProject();
    }

    @Override
    public @NotNull PsiElement getPsiElement() {
        return this.psiMethodCallExpression;
    }

    @Override
    public PsiType getPsiType() {
        PsiMethod psiMethod = psiMethodCallExpression.resolveMethod();
        if (Objects.isNull(psiMethod)){
            return null;
        }
        PsiType returnType = psiMethod.getReturnType();
        return returnType;
    }
}
